package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.AdSensitivePageRequestDto;
import com.itheima.admin.mapper.AdSensitiveMapper;
import com.itheima.admin.pojo.AdSensitive;
import com.itheima.admin.service.AdSensitiveService;
import com.itheima.common.vo.PageResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 分页查询敏感词信息
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo pageList(AdSensitivePageRequestDto dto) {
        //创建分页查询对象
        IPage<AdSensitive> iPage = new Page<>(dto.getPage(), dto.getSize());
        //构建条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        //敏感词名称模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(dto.getName()), AdSensitive::getSensitives, dto.getName());
        //执行并组装返回
        //IPage<AdSensitive> page = page(iPage, queryWrapper);
        page(iPage, queryWrapper);
        log.info("分页查询敏感词成功");

        //return PageResultVo.pageResult(page.getPages(), page.getSize(), page.getTotal(), page.getRecords());
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 新增敏感词
     *
     * @param AdSensitive
     * @return
     */
    @Override
    public Boolean add(AdSensitive AdSensitive) {
        boolean save =false;
        if (AdSensitive.getSensitives().length()<=10) {
            //补充数据--是否默认asDefault和创建时间
            AdSensitive.setCreatedTime(LocalDateTime.now());
            //执行新增操作
            save = save(AdSensitive);
            log.info("新增敏感词成功");
        }
        return save;
    }
}
